package com.newtouch.bxzs.business.personal.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Validator;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.newtouch.bxzs.business.enclosure.service.AttachmentService;
import com.newtouch.bxzs.business.personal.mapper.PersonalMapper;
import com.newtouch.bxzs.business.personal.model.ModifyUserPersonalInfoVO;
import com.newtouch.bxzs.business.personal.model.PersonalVisibleVO;
import com.newtouch.bxzs.business.personal.model.UserPersonalInfoBO;
import com.newtouch.bxzs.business.personal.service.PersonalService;
import com.newtouch.bxzs.business.product.mapper.UserMapper;
import com.newtouch.bxzs.common.base.BaseErrorEnum;
import com.newtouch.bxzs.common.base.BaseException;
import com.newtouch.bxzs.common.base.em.AttachmentEnum;
import com.newtouch.bxzs.common.base.model.User;
import com.newtouch.bxzs.common.base.model.vo.EnclosureVO;
import com.newtouch.bxzs.common.base.util.MD5Util;
import com.newtouch.bxzs.common.base.util.UserDetailUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * 用户个人信息service实现类
 *
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2020年12月28日</td>
 * <td>xiongdianqiang</td>
 * <td>Create</td>
 * </tr>
 * </table>
 *
 * @author xiongdianqiang
 *
 * @version 1.0.0
 * @since 1.0.0
 */
@Service
class PersonalServiceImpl implements PersonalService {
	@Autowired
	private PersonalMapper personalMapper;

	@Autowired
	private AttachmentService attachmentService;

	@Autowired
	private UserMapper userMapper;

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.newtouch.bxzs.business.personal.service.UserService#getUserInfoByCode
	 * (java. lang.String)
	 */
	@Override
	public UserPersonalInfoBO getPersonalDetail() {
		String userId = Objects.requireNonNull(UserDetailUtil.getUserDetail()).getUserId();
		if (userId == null) {
			throw new BaseException(BaseErrorEnum.REQUEST_DATA_NULL);
		}
		// 根据id查询用户信息
		User user = personalMapper.selectById(userId);
		// 获取岗位
		String positionName = personalMapper.selectPositionNameByUserId(userId);
		if (BeanUtil.isEmpty(user)) {
			throw new BaseException(BaseErrorEnum.USER_NOT_EXISTS);
		}
		UserPersonalInfoBO userPersonalInfoBO = new UserPersonalInfoBO();
		// 将用户信息装换为userPersonalInfoBO
		BeanUtil.copyProperties(user, userPersonalInfoBO);
		userPersonalInfoBO.setUserPosition(positionName);
		/* 获取附件 */
		HashMap<String, List<String>> result = attachmentService.getEnclosureList(new EnclosureVO(userId, null));
		if (result != null) {
			// 设置头像地址
			List<String> list = result.get(String.valueOf(AttachmentEnum.DETAIL_PICTURE.getCode()));
			if (list != null) {
				for (String url : list) {
					userPersonalInfoBO.setAttachmentUrl(url);
				}
			}
		}
		return userPersonalInfoBO;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.newtouch.bxzs.business.personal.service.UserService#
	 * updateUserInfoByCode(java. lang.String,
	 * com.newtouch.bxzs.business.personal.model.UserVO)
	 */
	@Override
	public void updatePersonalDetailById(ModifyUserPersonalInfoVO modifyUserPersonalInfoVO) {
		if (modifyUserPersonalInfoVO == null) {
			throw new BaseException(BaseErrorEnum.REQUEST_DATA_NULL);
		}
		String userId = Objects.requireNonNull(UserDetailUtil.getUserDetail()).getUserId();
		// 根据id查询用户信息
		User user = personalMapper.selectById(userId);
		// 更新个人信息
		if (!BeanUtil.isEmpty(user)) {
			if (!StringUtils.isEmpty(modifyUserPersonalInfoVO.getUserIntroduction())
					&& modifyUserPersonalInfoVO.getUserIntroduction().length() > 200) {
				throw new BaseException("个人简介不能超过200字符，请重新输入");
			}
			if (!StringUtils.isEmpty(modifyUserPersonalInfoVO.getUserDetailAddress())
					&& modifyUserPersonalInfoVO.getUserDetailAddress().length() > 200) {
				throw new BaseException("详细地址不能超过200字符，请重新输入");
			}
			if (!StringUtils.isEmpty(modifyUserPersonalInfoVO.getUserPhone())
					&& !Validator.isMobile(modifyUserPersonalInfoVO.getUserPhone())) {
				throw new BaseException("手机号格式不正确，请重新输入");
			}
			if (!StringUtils.isEmpty(modifyUserPersonalInfoVO.getUserEmail())
					&& !Validator.isEmail(modifyUserPersonalInfoVO.getUserEmail())) {
				throw new BaseException("邮箱格式不正确，请重新输入");
			}
			BeanUtils.copyProperties(modifyUserPersonalInfoVO, user);
			personalMapper.updateById(user);
		}
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.newtouch.bxzs.business.personal.service.PersonalService#
	 * updatePersonalCardById (java.lang.String,
	 * com.newtouch.bxzs.business.personal.model.PersonalVisibleVO)
	 */
	@Override
	public void updatePersonalCardById(PersonalVisibleVO personalVisibleVO) {
		if (personalVisibleVO == null) {
			throw new BaseException(BaseErrorEnum.REQUEST_DATA_NULL);
		}
		String userId = Objects.requireNonNull(UserDetailUtil.getUserDetail()).getUserId();
		// 根据id查询用户信息
		User user = personalMapper.selectById(userId);
		// 更新个人名片
		if (!BeanUtil.isEmpty(user)) {
			BeanUtils.copyProperties(personalVisibleVO, user);
			personalMapper.updateById(user);
		}
	}

	@Override
	public String getUserId(String userJobNumber) {
		User user = personalMapper.selectOne(new QueryWrapper<User>().eq("USER_JOB_NUMBER", userJobNumber));
		if (user == null){
			throw new BaseException(BaseErrorEnum.USER_NOT_EXISTS);
		}
		else {
			return user.getUserId();
		}
	}

	@Override
	public Integer modifyHandPass(String handPass, String confirmHandPass) {
		// 判断两次手势密码是否一致
		if (!handPass.equals(confirmHandPass)) {
			throw new BaseException(BaseErrorEnum.PASSWORD_DIFFERENT);
		}
		// 当前用户Id
		String userId = Objects.requireNonNull(UserDetailUtil.getUserDetail()).getUserId();
		// 获取用户信息,设置手势密码
		User user = userMapper.selectById(userId);
		// 手势密码加密
		user.setUserLoginHandPass(MD5Util.getMD5ofStr(handPass));
		user.setUpdateTime(new Date());
		user.setUpdateUserId(userId);

		return userMapper.update(user, new QueryWrapper<User>().eq("USER_ID", userId));
	}

	@Override
	public Integer enableHandPass(Boolean handPassEnable) {
		if (handPassEnable == null){
			return null;
		}
		// 当前用户Id
		String userId = Objects.requireNonNull(UserDetailUtil.getUserDetail()).getUserId();
		// 当前用户信息
		User user = userMapper.selectOne(new QueryWrapper<User>().eq("USER_ID",userId));
		user.setEnableHandPass(handPassEnable);
		user.setUpdateUserId(userId);
		user.setUpdateTime(new Date());
		// 开启/关闭手势密码
		return userMapper.update(user,new QueryWrapper<User>().eq("USER_ID",userId));
	}

	@Override
	public Boolean checkLoginPassword(String password) {
		if (StringUtils.isEmpty(password)){
			return null;
		}
		String userId = Objects.requireNonNull(UserDetailUtil.getUserDetail()).getUserId();
		String loginPassword = MD5Util.getMD5ofStr(password);
		User user = userMapper.selectOne(new QueryWrapper<User>().eq("USER_ID",userId).eq("USER_LOGIN_PASSWORD",loginPassword));
		return !BeanUtil.isEmpty(user);
	}

	@Override
	public Boolean checkHandPass(String password) {
		if (StringUtils.isEmpty(password)){
			return null;
		}
		String userId = Objects.requireNonNull(UserDetailUtil.getUserDetail()).getUserId();
		String handPass = MD5Util.getMD5ofStr(password);
		User user = userMapper.selectOne(new QueryWrapper<User>().eq("USER_ID",userId).eq("USER_LOGIN_HAND_PASS",handPass));
		return !BeanUtil.isEmpty(user);
	}

	@Override
	public Integer updateLoginPassword(String password, String confirmPassword) {
		// 判断两次密码是否一致
		if (!password.equals(confirmPassword)) {
			throw new BaseException(BaseErrorEnum.PASSWORD_DIFFERENT);
		}
		// 当前用户Id
		String userId = Objects.requireNonNull(UserDetailUtil.getUserDetail()).getUserId();
		// 获取用户信息,修改密码
		User user = userMapper.selectById(userId);
		// 密码加密
		user.setUserLoginPassword(MD5Util.getMD5ofStr(password));
		user.setUpdateTime(new Date());
		user.setUpdateUserId(userId);

		return userMapper.update(user, new QueryWrapper<User>().eq("USER_ID", userId));
	}
}
